package com.xy.servicemanage.utils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import com.xy.common.util.Configure;
import com.xy.common.util.HttpSender;
import com.xy.core.util.SpringBeanUtils;
import com.xy.manage.model.Trequest;
import com.xy.manage.service.TrequestService;
import com.xy.servicemanage.db.PoolManager;

public class RequestScaner {
	public RequestScaner() {
		// TODO Auto-generated constructor stub
	}

	public void start() {
		TrequestService req = (TrequestService) SpringBeanUtils
				.getBean("trequestService");
		// 前置：1，后置：2
		String currentServer = Configure.getProperty("currentServer");
		if ("1".equals(currentServer))
			new Thread(new ScanFrontThread(req)).start();
		else
			new Thread(new ScanBackThread(req)).start();
	}

	public static List<Long> sended_front = new ArrayList<Long>();
	public static List<Long> sended_back = new ArrayList<Long>();

	public static List<Trequest> getTrequest() {
		List<Trequest> list = new ArrayList<Trequest>();
		try {
			Connection conn = PoolManager.getConnection("local1");
			PreparedStatement ps = conn
					.prepareStatement("select * from t_request");
			ResultSet rs = ps.executeQuery();

			while (rs.next()) {
				Long rid = rs.getLong("rid");
				String res  = rs.getString("response");
				Trequest t = new Trequest();
				t.setRid(rid);
//				t.setResponse(res);
				list.add(t);
			}
			rs.close();
			ps.close();
			conn.close();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return list;
	}

	class ScanFrontThread implements Runnable {
		TrequestService req;

		public ScanFrontThread(TrequestService req) {
			this.req = req;
		}

		@Override
		public void run() {
			while (true) {
				try {

					List<Trequest> list = getTrequest();
					List<Long> tmp = new ArrayList<Long>();
					for (Trequest i : list) {
						tmp.add(i.getRid());
					}
					Iterator<Long> it = sended_front.iterator();
					while (it.hasNext()) {
						Long r = it.next();
						if (!tmp.contains(r)) {
							it.remove();
						}
					}

					for (Trequest t : list) {
						if (t.getResponse() != null) {
							if (!sended_front.contains(t.getRid())) {
								HttpSender.notify(t.getRid());
								sended_front.add(t.getRid());
							}
						}
					}

					Thread.sleep(1000);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
	}

	class ScanBackThread implements Runnable {
		TrequestService req;

		public ScanBackThread(TrequestService req) {
			this.req = req;
		}

		@Override
		public void run() {
			while (true) {
				try {
					List<Trequest> list = getTrequest();
					List<Long> tmp = new ArrayList<Long>();
					for (Trequest i : list) {
						tmp.add(i.getRid());
					}

					Iterator<Long> it = sended_back.iterator();
					while (it.hasNext()) {
						Long r = it.next();
						if (!tmp.contains(r)) {
							it.remove();
						}
					}

					for (Trequest t : list) {
						if (t.getResponse() == null) {
							if (!sended_back.contains(t.getRid())) {
								HttpSender.query(t.getRid());
								sended_back.add(t.getRid());
							}
						}
					}
					Thread.sleep(1000);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}

		}
	}
}
